using System;
using System.Collections.Generic;
using System.Text;

namespace GuavaData
{
    public class Insert : Operator
    {
        // DML statement for inserting rows into a table

        #region Fields
        string table;
        public string Table
        {
            get { return table; }
            set { table = value; }
        }

        List<string> columns;
        public List<string> Columns
        {
            get { return columns; }
            set { columns = value; }
        }

        Operator child;
        public Operator Child
        {
            get { return child; }
            set { child = value; }
        }
        #endregion Fields

        public Insert(string table, IEnumerable<string> columns, Operator query)
        {
            this.table = table;
            this.columns = new List<string>(columns);
            this.child = query;
            this.parent = null;

            // Dummy check
            if (query.exposedColumns == null)
                throw new Exception("Query to produce rows does not have any exposed columns, and is of type " + query.GetType().Name + ".");
            //if (this.columns.Count != query.exposedColumns.Count)
            //    throw new Exception("Column counts between expected columns " + this.columns.Count.ToString() + " and query exposed columns " + query.exposedColumns.Count + " do not match.");
        }
        
        public override void NotifyChange(Operator old, Operator newOp)
        {
            if (this.child == old)
            {
                this.child = newOp;
                newOp.parent = this;
            }
        }
        public override bool isQuery
        {
            get
            {
                return false;
            }
        }
        public override SqlText Visit(Provider qv)
        {
            return qv.Transform(this);
        }
        public override void Visit(Transform t, Command c)
        {
            t.Expand(this, c);
        }
        public override Operator Clone()
        {
            return new Insert(this.table, this.columns, this.child.Clone());
        }

        public override void Visit(Channel c, string table)
        {
            c.MapAliases(this, table);
        }

    }
}
